/**
 * way:用于可以无限滚动的工具函数
 * */
// 兼容ie8及以上
export default function (key=0){
//offsetTop最近的浮动元素的高度
// https://blog.csdn.net/weixin_42614080/article/details/90485219
let broadcast_wrapper_box = document.querySelector('.broadcast_wrapper_box') as HTMLElement
let box = document.querySelector('#broadcast_list') as HTMLElement
let collapse = document.querySelector('.ant-collapse') as HTMLElement
let Index:number = key
let collapseccItem = document.querySelectorAll('.collapsecc')[Index] as HTMLElement
//当前播放项距离顶部的高度
let scrotop = collapseccItem?.offsetTop + collapse?.offsetTop
//已经卷起来的高度
let scro:number = broadcast_wrapper_box.scrollTop
//话术列表高度小于等于整体框架高度怎不进行滚动,
//如果话术列表高于整体框架高度,但在播放的该项和下面其他项加在一起都无法 大于等于整体框架高度 则滚动条直接到底部
//自身小于框架高度,且是最后一个
//上面两种情况都不满足的时候,css3动画滚动到底部

//抽取为统一的滚动并注销事件
  function offsetReset(val:number,time=1,scrollT:number){
    box.style.transition = `all ${time}s`;
    box.style.transform = `translateY(${val}px)`;
    let tim = setTimeout(()=>{
      box.style.removeProperty('transition')
      box.style.removeProperty('transform')
      broadcast_wrapper_box.scrollTop = scrollT
      clearTimeout(tim)
    },1000)
  }

  function scrollTopFun(){
    //能卷的高度(这里不用scrotop是因为可能scrotop的值大于我能卷起的高度)
    let scroHeight:number = collapse.offsetHeight - broadcast_wrapper_box.offsetHeight
    let templScroHeight:number = scroHeight
    if (scro > scroHeight){
      templScroHeight = scroHeight - scro
    }else {
      if (scro > 0){
        templScroHeight = scroHeight - scro
      }
    }
    if (scro  == scroHeight) {
      return;
    }
    templScroHeight = -templScroHeight
    offsetReset(templScroHeight,1,scroHeight)
  }
  if (collapse.offsetHeight <= broadcast_wrapper_box.offsetHeight){
    return
  }else {
    if (collapse.offsetHeight - scrotop < broadcast_wrapper_box.offsetHeight){
      scrollTopFun()
      return;
    }else {
      if (collapseccItem.offsetHeight < broadcast_wrapper_box.offsetHeight && document.querySelectorAll('.collapsecc').length == Index){
        scrollTopFun()
        return;
      }
    }
  }
  let tranHeight = scrotop
  if (scro > scrotop){
    tranHeight = scrotop - scro
  }else {
    if (scro > 0){
      tranHeight = scrotop - scro
    }
  }
  if (scro  == scrotop) {
    return;
  }
  tranHeight = -tranHeight
  offsetReset(tranHeight,1,scrotop)
}
